
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:zfth/api/AlarmApi.dart';
import 'package:zfth/api/RepairApi.dart';

import '../../components/form/LFormLogic.dart';
import '../../components/form/LFormPage.dart';
import '../../components/list/LCellListLogic.dart';
import '../../utils/Settings.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import '../../utils/StoreData.dart';



class RepairAddFormPage extends StatefulWidget{

  const RepairAddFormPage({super.key});

  @override
  State<RepairAddFormPage> createState() =>RepairAddFormState();

}

class RepairAddFormState extends State<RepairAddFormPage> with AutomaticKeepAliveClientMixin{

  String formTag = "RepairAddForm";
  late final LFormLogic lFormLogic;
  bool keepAlive = true;
  List formItems = [
    {"field":'deviceNo',"label":'设备',"component":'select',"readonly":false,"required":true,"isShowLabel":true,"dictCode":Settings.dictCodes["设备"]},
    {"field":'repairCause',"label":'报修原因',"component":'input',"readonly":false,"required":true,"isShowLabel":true,"maxLines":3},

  ];
  Map formData={};
  late String returnTag;


  @override
  void initState() {
    lFormLogic = Get.put(LFormLogic(),tag:formTag);
    lFormLogic.init(formItems: formItems, formData: formData,tag: formTag,onChangeCallback: onChange);

    super.initState();
  }





  @override
  void dispose() {
    Get.delete<LFormLogic>(tag: formTag);
    super.dispose();
  }

  @override
  bool get wantKeepAlive => keepAlive;
  bool isShowButton = true;

  ///内容改变后的回调
  void onChange(re){}

  void submit() async{

    if((lFormLogic.getFormKey().currentState as FormState).validate()){
      EasyLoading.show();
      dynamic res =  await RepairApi.repair(formData);
      if(res["success"]){
        EasyLoading.dismiss();
        EasyLoading.showSuccess(res["message"]);
        setState(() {isShowButton = false;});
      }else{
        EasyLoading.dismiss();
        EasyLoading.showError(res["message"]);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
        backgroundColor: Settings.backgroundColor,
        appBar: AppBar(
          title: const Text("终端报修"),
          centerTitle: true,
          elevation: 0,
        ),
        body: SingleChildScrollView(
          child: Stack(
            children: [
              Column(
                children: [
                  LFormPage(tag:formTag),
                  Padding(
                      padding: const EdgeInsets.all(10),
                      child: Row(
                        children: [
                          Expanded(
                            child: Visibility(
                              visible: isShowButton,
                              child: SizedBox(
                                height: 50,
                                child: OutlinedButton(
                                  style: ButtonStyle(
                                    backgroundColor: MaterialStateProperty.resolveWith<Color>((states) {
                                      return Settings.primaryColor;
                                    }),
                                    foregroundColor: MaterialStateProperty.resolveWith<Color>((states) {
                                      return Colors.white;
                                    }),
                                  ),
                                  onPressed:submit,
                                  child: const Text('提交',style: TextStyle(fontSize: 17,)),

                                ),
                              ),
                            ),
                          )
                        ],
                      )
                  )
                ],
              ),
            ],
          ),
        )




    );
  }


}